<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/2/22
 * Time: 14:37
 */

namespace app\login\service;


use app\lib\exception\TokenException;
use app\login\model\Popularize;

class DelService
{
    public function del($data,$scope)
    {
        $this->checkScope($scope);
        $ids = $this->getDelId($data);
        $result = Popularize::destroy($ids);
        if($result){
            return [
              'code' => 200,
              'msg' => '删除成功'
            ];
        }else{
            return [
                'code' => 400,
                'msg' => '删除失败'
            ];
        }
    }

    private function checkScope($scope)
    {
        if($scope < 32){
            throw new TokenException([
                'msg' => '没有权限',
                'code' => '500'
            ]);
        }
    }

    /**
     * @param $data 前端传过来的数组
     * @return array 返回所有数组的id集合
     */
    private function getDelId($data)
    {
        $ids = [];
        foreach ($data as $value){
            if(array_key_exists('id',$value)){
                array_push($ids, $value['id']);
            }
        }
        return $ids;
    }
}